import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
  {
    path:'/',
    redirect:'/Dashboard'
  },
  {
    path: '/Dashboard',
    name: 'Dashboard',
    redirect:'/Dashboard/welcome',
    component:()=>import('../views/Home/Dashboard.vue'),
    children:[
      // {
      //   path:'/welcome',
      //   redirect:'/Dashboard/welcome'
      // },
      {
        path:'welcome',
        component:()=>import('../views/Home/welcome.vue')
      },
      {
        path:'user',
        component:()=>import('../views/user/index.vue'),
        children:[
          {
            path:'index',
            component:()=>import('../views/user/index.vue'),
          }
        ]
      },
      {
        path:'cinema',
        component:()=>import('../views/cinema/index.vue'),
        children:[
          {
            path:'index',
            component:()=>import('../views/cinema/index.vue'),
          }
        ]
      },
      {
        path:'college',
        component:()=>import('../views/college/index.vue'),
        children:[
          {
            path:'index',
            component:()=>import('../views/college/index.vue'),
          }
        ]
      },
      {
        path:'film',
        component:()=>import('../views/film/index.vue'),
        children:[
          {
            path:'index',
            component:()=>import('../views/film/index.vue'),
          }
        ]
      }
    ]
  },
  {
    path:'/login',
    name:'Login',
    component:()=>import('../views/Login/index.vue')
  },
  ]
})
// 路由全局前置守卫
router.beforeEach((to,from,next)=>{
  // 验证 token 只有 token 存在的时候才能跳转到内容页面
  let token = localStorage.getItem("token")
  // console.log(token);
  if(token || to.path === '/login'){
    next()
  }else{
    // 判断有无登录
    // const userinfo = JSON.parse(localStorage.getItem('vuex'))
    if(!token){
      next('/login')
    }else{
      next(true)
    }
    // next('/Login')
  }
})
export default router
